package myApp.commonsware.cwac.thumbnail;

import com.commonsware.cwac.adapter.AdapterWrapper;

import myApp.commonsware.cwac.cache.SimpleWebImageCache;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;


public class ThumbnailAdapter extends AdapterWrapper {
	private static final String TAG = "ThumbnailAdapter";
	private int[] imageIds;
	private SimpleWebImageCache<ThumbnailBus, ThumbnailMessage> cache = null;
	private Activity host = null;

	// Bitmap bdRounded;

	/**
	 * Constructor wrapping a supplied ListAdapter
	 */
	public ThumbnailAdapter(Activity host, ListAdapter wrapped,
			SimpleWebImageCache<ThumbnailBus, ThumbnailMessage> cache,
			int[] imageIds) {
		super(wrapped);

		this.host = host;
		this.imageIds = imageIds;
		this.cache = cache;

		cache.getBus().register(getBusKey(), onCache);
	}

	public void close() {
		cache.getBus().unregister(onCache);
	}
	
	public void clearCache(String key){
		cache.remove(key);
	}

	/**
	 * Get a View that displays the data at the specified position in the data
	 * set. In this case, if we are at the end of the list and we are still in
	 * append mode, we ask for a pending view and return it, plus kick off the
	 * background task to append more data to the wrapped adapter.
	 * 
	 * @param position
	 *            Position of the item whose data we want
	 * @param convertView
	 *            View to recycle, if not null
	 * @param parent
	 *            ViewGroup containing the returned View
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View result = super.getView(position, convertView, parent);

		processView(result);

		return (result);
	}

	public void processView(View row) {
		if(row != null){
		for (int imageId : imageIds) {
			ImageView image = (ImageView) row.findViewById(imageId);
			if (image != null && image.getTag() != null) {
				ThumbnailMessage msg = cache.getBus()
						.createMessage(getBusKey());

				msg.setImageView(image);
				msg.setUrl(image.getTag().toString());
				try {
					cache.notify(msg.getUrl(), msg);					
				} catch (Throwable t) {
					Log.e(TAG, "Exception trying to fetch image", t);
				}
			}
		}
		}
	}

	private String getBusKey() {
		return (toString());
	}

	private ThumbnailBus.Receiver<ThumbnailMessage> onCache = new ThumbnailBus.Receiver<ThumbnailMessage>() {
		public void onReceive(final ThumbnailMessage message) {
			final ImageView image = message.getImageView();
			
			/** 
			* @author: HaiTC
			* @since: June 28, 2011
			* @Description: lay progressbar khi load hinh anh
			*/
//			final ProgressBar pbLoading = message.getPbLoading();
//			final TextView tvNotifyNonImage = message.getTvNotifyNonImage();
			
			host.runOnUiThread(new Runnable() {
				public void run() {
					if (image.getTag() != null
							&& image.getTag().toString()
									.equals(message.getUrl())) {
						BitmapDrawable bd = (BitmapDrawable) cache.get(message
								.getUrl());
						// Bitmap bdRounded =
						// getRoundedCornerBitmap(bd.getBitmap());
						if (bd != null) {
							// image.setImageBitmap(bdRounded);
							// } else {
							// if (bd != null) {
							image.setImageDrawable(bd);
						}
					}
				}
			});
		}
	};

	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

		if (bitmap != null) {
				Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
						.getHeight(), Config.ARGB_8888);
				Canvas canvas = new Canvas(output);

				final int color = 0xff424242;
				final Paint paint = new Paint();
				final Rect rect1 = new Rect(0, 0, bitmap.getWidth(), bitmap
						.getHeight());
				final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap
						.getHeight());
//				final Rect rect2 = new Rect(0, 0, bitmap.getWidth() + 4, bitmap
//						.getHeight() + 4);
//				final RectF rectF = new RectF(rect2);
//				final float roundPx = 0;

				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(color);
//				paint.setStrokeWidth(1);
				canvas.drawColor(Color.WHITE);
//				paint.setStyle(Paint.Style.STROKE);
//				canvas.drawRect(rectF, paint);

				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(bitmap, rect, rect1, paint);

			return output;
		} else {
			return bitmap;
		}
	}
}